## Replication File for: Lily Tsai and Yiqng Xu. "Outspoken Insiders:
## Political Connections and Citizen Participation in Authoritarian
## China"

## Matching (Table 4)


rm(list=ls(all=TRUE))
library(foreign)
library(Matching)
setwd("~/Dropbox/Projects/insider/Replication")

##############################
# Urban
##############################


d<-read.dta("match_urban.dta",convert.factors=F)
head(d)
table(d$govconn)

Y<-"compgov"
D<-"govconn"
Covariates<-c("male","age","agebin","govoff","eduyr","ccp","hukou","distrid")
Covars1<-c("male","agebin","govoff","eduyr","ccp","hukou","distrid") # exact match
Covars2<-c("male","age","govoff","eduyr","ccp","hukou","distrid")  # nonexact match
Adj.var<-c("male","age","govoff","eduyr","ccp","hukou") # for bias adjustment
MatchM<-c(1,2,4,8)


# storage
est<-matrix(NA,3,5) # exact, M=1, M=2, M=4, M=10
  
# exact matches
mat.exact<-Match(Y=d[,Y],Tr=d[,D],X=d[,Covars1],estimand="ATT", M=1, exact=1,Var.calc=1)
est[1,1]<-round(mat.exact$est,3)
est[2,1]<-paste("(",round(mat.exact$se,3),")",sep="")
est[3,1]<-paste("[",mat.exact$wnobs,"]",sep="")

# nonexact matches (but still matches distrid)
for (j in 1:4) {
  mat.nonexact<-Match(Y=d[,Y],Tr=d[,D],X=d[,Covars2],Z=d[,Adj.var],
                      exact=c(rep(0,(length(Covars2)-1)),1),
                      estimand="ATT", M=MatchM[j], BiasAdjust=1,Var.calc=1)
  est[1,(j+1)]<-round(mat.nonexact$est,3)
  est[2,(j+1)]<-paste("(",round(mat.nonexact$se,3),")",sep="")
  est[3,(j+1)]<-paste("[",mat.nonexact$wnobs,"]",sep="")
}
est


##############################
# Rural
##############################


d<-read.dta("match_rural.dta")


Y<-"compl"
D<-"govconn"
Covariates<-c("male","age","agebin","leader","eduyr","ccp","v_id")
Covars1<-c("male","agebin","leader","eduyr","ccp","v_id") # exact match
Covars2<-c("male","age","leader","eduyr","ccp","v_id")  # nonexact match
Adj.var<-c("male","age","leader","eduyr","ccp")
MatchM<-c(1,2,4,8)

# storage
est<-matrix(NA,3,5) # exact, M=1, M=2, M=4, M=10

# exact matches
mat.exact<-Match(Y=d[,Y],Tr=d[,D],X=d[,Covars1],estimand="ATT", M=1, exact=1,Var.calc=1)
est[1,1]<-round(mat.exact$est,3)
est[2,1]<-paste("(",round(mat.exact$se,3),")",sep="")
est[3,1]<-paste("[",mat.exact$wnobs,"]",sep="")

# nonexact matches
for (j in 1:4) {
  mat.nonexact<-Match(Y=d[,Y],Tr=d[,D],X=d[,Covars2],Z=d[,Adj.var],
                      exact=c(rep(0,(length(Covars2)-1)),1),
                      estimand="ATT", M=MatchM[j], BiasAdjust=1,Var.calc=1)
  est[1,(j+1)]<-round(mat.nonexact$est,3)
  est[2,(j+1)]<-paste("(",round(mat.nonexact$se,3),")",sep="")
  est[3,(j+1)]<-paste("[",mat.nonexact$wnobs,"]",sep="")
}
est

